<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-if="hasPermission(CheckActionCodeAdd)">
          新增角色
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: handleEdit.bind(null, record),
              auth: CheckActionCodeEdit,
            },
            {
              icon: 'clarity:note-edit-line',
              label: '权限',
              onClick: handlePemissionEdit.bind(null, record),
              auth: CheckActionCodeEdit,
            },
            {
              icon: 'clarity:note-edit-line',
              label: '菜单',
              onClick: handleMenuEdit.bind(null, record),
              auth: CheckActionCodeEdit,
            },
          ]"
        />
      </template>
    </BasicTable>
    <RoleNavigationModal @register="registerNavigationModal" @success="handleSuccess" />
    <RoleModal @register="registerModal" @success="handleSuccess" />
    <RolePermissionModal @register="registerPermissionModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getRoleList } from '/@/api/sys/system'; 
  import RoleModal from './RoleModal.vue';
  import RoleNavigationModal from './RoleNavigationModal.vue';
  import RolePermissionModal from './RolePermissionModal.vue';
  import { columns, searchFormSchema } from './role.data';
  import { useModal } from '/@/components/Modal';
  import { useRouter } from 'vue-router';
  import { usePermission } from '/@/hooks/web/usePermission';
  export default defineComponent({
    name: 'RoleManagement',
    components: { BasicTable, TableAction, RoleNavigationModal, RoleModal, RolePermissionModal },
    setup() {
      const router = useRouter();
      const { currentRoute } = router;
      const routeName: string = currentRoute.value!.name;
      const CheckActionCodeAdd = routeName + '_' + 'Add';
      const CheckActionCodeEdit = routeName + '_' + 'Edit';
      const { hasPermission } = usePermission();
      const [registerModal, { openModal }] = useModal();
      const [registerNavigationModal, { openModal: openUpdateNavigatioinModal }] = useModal();
      const [registerPermissionModal, { openModal: openUpdatePermissionModal }] = useModal();
      const [registerTable, { reload }] = useTable({
        title: '角色列表',
        api: getRoleList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        beforeFetch(params) {
          if (params.name) {
            params.filter = params.name;
          }
          return params;
        },
        pagination: { current: 0 },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });
      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }
      function handlePemissionEdit(record: Recordable) {
        openUpdatePermissionModal(true, {
          record,
          isUpdate: true,
        });
      }
      function handleMenuEdit(record: Recordable) {
        openUpdateNavigatioinModal(true, {
          record,
          isUpdate: true,
        });
      }

      function handleDelete(record: Recordable) {
        console.log(record);
      }

      function handleSuccess() {
        reload();
      }
      return {
        registerTable,
        registerModal,
        registerPermissionModal,
        registerNavigationModal,
        handleCreate,
        handleEdit,
        handleMenuEdit,
        handleDelete,
        handlePemissionEdit,
        handleSuccess,
        hasPermission,
        CheckActionCodeAdd,
        CheckActionCodeEdit,
      };
    },
  });
</script>
